namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceHolidayPageEdit : Page
    {
        protected DropDownList HolidayCityDdl;
        protected TextBox HolidayFeatureTxb;
        protected TextBox HolidayNameTxb;
        protected DropDownList HolidayProvinceDdl;
        protected TextBox HolidayTimeTxb;
        protected Image iResource;
        protected Label lbHolidayCity;
        protected Label lbHolidayFeature;
        protected Label lbHolidayName;
        protected Label lbHolidayProvince;
        protected Label lbHolidayTime;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["HolidayId"]);
                ResourceHoliday resourceHoliday = ManageResource.GetResourceHolidayInfoViaId(id);
                this.HolidayNameTxb.Text = resourceHoliday.HolidayName;
                this.HolidayTimeTxb.Text = resourceHoliday.HolidayTime;
                this.HolidayFeatureTxb.Text = resourceHoliday.HolidayFeature;
                for (int i = 0; i < this.HolidayProvinceDdl.Items.Count; i++)
                {
                    if (this.HolidayProvinceDdl.Items[i].Value == resourceHoliday.HolidayProvince.ToString())
                    {
                        this.HolidayProvinceDdl.SelectedIndex = i;
                    }
                }
                this.HolidayCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceHoliday.HolidayProvince);
                this.HolidayCityDdl.DataTextField = "regionName";
                this.HolidayCityDdl.DataValueField = "regionId";
                this.HolidayCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.HolidayCityDdl.Items.Count; i++)
                {
                    if (this.HolidayCityDdl.Items[i].Value == resourceHoliday.HolidayCity.ToString())
                    {
                        this.HolidayCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.HolidayCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.HolidayCityDdl.Items.Count; i++)
                {
                    if (this.HolidayCityDdl.Items[i].Value == resourceHoliday.HolidayCity.ToString())
                    {
                        this.HolidayCityDdl.SelectedIndex = i;
                    }
                }
            }
        }

        private void HolidayProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.HolidayCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.HolidayProvinceDdl.SelectedItem.Value));
            this.HolidayCityDdl.DataTextField = "regionName";
            this.HolidayCityDdl.DataValueField = "regionId";
            this.HolidayCityDdl.DataBind();
        }

        private void InitializeComponent()
        {
            this.HolidayProvinceDdl.SelectedIndexChanged += new EventHandler(this.HolidayProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.HolidayProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.HolidayProvinceDdl.DataTextField = "RegionName";
            this.HolidayProvinceDdl.DataValueField = "RegionId";
            this.HolidayProvinceDdl.DataBind();
            this.HolidayCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.HolidayProvinceDdl.SelectedItem.Value));
            this.HolidayCityDdl.DataTextField = "regionName";
            this.HolidayCityDdl.DataValueField = "regionId";
            this.HolidayCityDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strHolidayName = this.HolidayNameTxb.Text.Trim();
            string strHolidayTime = this.HolidayTimeTxb.Text.Trim();
            string strHolidayFeature = this.HolidayFeatureTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strHolidayName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.HolidayCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.HolidayCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceHolidayInfo(new Guid(base.Request.QueryString["HolidayId"]), strHolidayName, strHolidayTime, new Guid(this.HolidayProvinceDdl.SelectedItem.Value), cityId, strHolidayFeature);
                    base.Response.Redirect("~/Resource/ResourceHolidayPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strHolidayName != "")
            {
                if (!ManageResource.IsExistSameResourceHolidayInfo(strHolidayName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.HolidayCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.HolidayCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceHolidayInfo(strHolidayName, strHolidayTime, new Guid(this.HolidayProvinceDdl.SelectedItem.Value), cityId, strHolidayFeature);
                    base.Response.Redirect("~/Resource/ResourceHolidayPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbHolidayName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbHolidayProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayProvinceOverSea", language, path);
                this.lbHolidayCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayCityOverSea", language, path);
            }
            else
            {
                this.lbHolidayProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayProvince", language, path);
                this.lbHolidayCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayCity", language, path);
            }
            this.lbHolidayTime.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayTime", language, path);
            this.lbHolidayFeature.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "lbHolidayFeature", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHolidayPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

